import java.util.*;

public class 全排列 {
    //https://leetcode.cn/problems/permutations/
    //简单的递归, 用全局变量
    public static List<List<Integer>> ret;
    public static List<Integer> path;
    public static int n;
    public static boolean[] vis;

    public List<List<Integer>> permute(int[] nums) {
        n = nums.length;
        ret = new ArrayList<>();
        path = new ArrayList<>();
        vis = new boolean[n];
        dfs(0,nums);
        return ret;
    }

    public void dfs(int pos,int[] nums) {
        if(pos == n){
            List<Integer> cur = new ArrayList<>(path);
            ret.add(cur);
            return;
        }
        for(int i = 0;i < n;i++){
            if(!vis[i]){
                //标记已经选过了
                path.add(nums[i]);
                vis[i] = true;
                dfs(pos + 1,nums);
                //回溯
                path.remove(path.size() - 1);
                vis[i] = false;
            }
        }

    }
}
